package lc.栈;

import java.util.Stack;

/**
 * @description:
 * @author: qian
 * @createDate: 2021/10/12
 */
public class 剑指Offer30_包含min函数的栈 {


    Stack<Integer> s1;
    Stack<Integer> s2;
    public 剑指Offer30_包含min函数的栈() {


        s1 = new Stack<>();

        // s2是一个辅助栈来用栈顶存放当前s1里最小的元素
        s2 = new Stack<>();
    }

    public void push(int x) {

        s1.push(x);

        if (s2.isEmpty()) {
            s2.push(x);
        } else {
            if (s2.peek() >= x) {
                s2.push(x);
            } else {
                s2.push(s2.peek());
            }
        }
    }

    public void pop() {
        s1.pop();
        s2.pop();
    }

    public int top() {
        return s1.peek();
    }

    public int min() {
        return s2.peek();
    }

}
